package cn.learn.shiro;


import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
//自定义密码比较器
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        //获取用户输入的用户名和密码
        UsernamePasswordToken upt = (UsernamePasswordToken) token;
        String username = upt.getUsername();
        String password = new String(upt.getPassword());

//获取数据库里面存入的密码

        String credentials = (String) info.getCredentials();
        if (password.equals(credentials)){
            return true;
        }
        //对用户输入的密码进行MD5加密
        Md5Hash md5Hash = new Md5Hash(password, username, 1);
        String md5password = md5Hash.toString();
        return md5password.equals(credentials);
    }
}
